package com.ee.chat.api.controller;

import com.ee.chat.api.common.CommonService;
import com.ee.chat.common.bean.Result;
import com.ee.chat.common.service.IPushMessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("push")
public class PushController {
    private Logger log = LoggerFactory.getLogger(PushController.class);

    @Resource
    IPushMessageService pushMessageService;
    @Resource
    CommonService commonService;

    @RequestMapping("getNewestPushMessage")
    public Result getNewestPushMessage(@RequestParam String token){
        try {
            Integer userId = commonService.getUserId(token);
            return pushMessageService.getNewestPushMessage(userId);
        } catch(Exception e){
            log.error("【获取最新推送消息错误】", e);
        }
        return Result.returnFailure();
    }

    /**
     * 获取推送消息
     * @Author Shane
     * @Date 2019/10/22 22:27
     * @param
     * @return
     */
    @RequestMapping("getPushMessage")
    public Result getPushMessage(@RequestParam String token, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit){
        try {
            Integer userId = commonService.getUserId(token);
            return pushMessageService.getPushMessage(userId, page, limit);
        } catch(Exception e){
            log.error("【获取推送消息错误】", e);
        }
        return Result.returnFailure();
    }

    /**
     * 清除历史消息
     * @Author Shane
     * @Date 2020/4/20 16:38
     * @param
     * @return
     */
    @RequestMapping("clear")
    public Result clear(@RequestParam String token){
        try {
            Integer userId = commonService.getUserId(token);
            return pushMessageService.cleanPushMessage(userId);
        } catch(Exception e){
            log.error("【清除历史消息错误】", e);
        }
        return Result.returnFailure();
    }
}
